function []=testPropertiesCorrelationSelfCalibMatrix(numtries)

properties={'simpleObjective','stochasticnorm','eigvarsubspacestd'};
numproperties=size(properties,2);
datascatter=zeros(numtries,numproperties+1);
datanoisemag=zeros(numtries,1);

width=1024;height=768;
[ Fz, K,ps , badpoints,corrs, FFormatted, corrsFormatted,EFormatted,spacePts,widthm,heightm,FFormattedCLEAN,FCLEAN  ] = generateF( 0,0,1,40,0,5,0,0 ,width,height,0);


for i=1:numtries
    noisel=rand();
    datanoisemag(i,1)=noisel;
    [F,x1,x2]=addspecificnoisetoF(corrs{1,1},corrs{2,1},noisel);
    
    b = mean(funddist(F, [corrs{1,1} ; corrs{2,1}]));
    datascatter(i,1)=b;
    
    for j=1:numproperties
        fh=str2func(properties{j});
        datascatter(i,1+j)=  fh(F,width,height) ;
    end
    
    
    
    
    
end

for j=1:numproperties
    scatter(datanoisemag',datascatter(:,1+j));
    [r,p]=corrcoef(datanoisemag,datascatter(:,1+j));
    title(['properties: ' properties{j} ' - correlation was ' num2str(r(1,2)) ' and p value was ' num2str(p(1,2))]);
    display(['properties: ' properties{j} ' - correlation was ' num2str(r(1,2)) ' and p value was ' num2str(p(1,2))]);
    figure
end

scatter(datanoisemag',datascatter(:,1));
[r,p]=corrcoef(datanoisemag,datascatter(:,1));
title(['the geometric error and correlation was ' num2str(r(1,2)) ' and p value was ' num2str(p(1,2))]);
display(['the geometric error and correlation was ' num2str(r(1,2)) ' and p value was ' num2str(p(1,2))]);
end


function [d]=eigvarsubspacestd(F,w,h)
% this is minimum property
mscale=1;
numTries=1000;
xstart=w/2;
ystart=h/2;
fstart=w;


fstd=1* mscale;
xstd=(xstart/200)* mscale;
ystd=(ystart/200)* mscale;
skewstd=0.01*mscale;
arstd=0.01*mscale;


Ed=zeros(numTries,1);


focal=fstart;
ar=1;
skew=0;
xguess=xstart;
yguess=ystart;
Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];

    G=(Kguess')*F*Kguess;
    [V,D] = eig(G'*G);
    B=V(:,1:2);
    


for i=1:numTries
    focal=fstart+(randn()*fstd);
    ar=(1+rand()*arstd);
    skew=skewstd*randn();
    xguess=xstart+(randn()*xstd);
    yguess=ystart+(randn()*ystd);
    Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];
    
    
 
        G=(Kguess')*F*Kguess;
        [V,~] = eig(G'*G);
        vv= subspace(V(:,1:2),B);
        Ed(i,1)=vv(1,1);
        

  
end

d=std(Ed);


end


function [d]=stochasticnorm(F,w,h)
mscale=30;
[xstart,ystart,fstart,fstd,xstd,ystd,skewstd,arstd]=setupstdSforperturb(mscale,w,h,F);
numTries=2000;


Ed=zeros(numTries,1);


for i=1:numTries
    focal=fstart+(randn()*fstd);
    ar=(1+rand()*arstd);
    skew=skewstd*randn();
    xguess=xstart+(randn()*xstd);
    yguess=ystart+(randn()*ystd);
    Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];
    
    G=(Kguess')*F*Kguess;
    
    Ed(i,1)=norm(G,'fro');
    
end

d=mean(Ed);
end

function [d]=svectorvariance(F,w,h)
mscale=30;
[xstart,ystart,fstart,fstd,xstd,ystd,skewstd,arstd]=setupstdSforperturb(mscale,w,h,F);
numTries=2000;


Ed=zeros(numTries,1);


for i=1:numTries
    focal=fstart+(randn()*fstd);
    ar=(1+rand()*arstd);
    skew=skewstd*randn();
    xguess=xstart+(randn()*xstd);
    yguess=ystart+(randn()*ystd);
    Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];
    
    G=(Kguess')*F*Kguess;
    [U,S,V] = svd(G);
    vv=V(:,3)/V(3,3);
    Ed(i,1)=vv(1,1);
    
end

d=std(Ed);
end

function [d]=simpleObjective(F,w,h)
mscale=30;
[xstart,ystart,fstart,fstd,xstd,ystd,skewstd,arstd]=setupstdSforperturb(mscale,w,h,F);
numTries=2000;


Ed=zeros(numTries,1);


for i=1:numTries
    focal=fstart+(randn()*fstd);
    ar=(1+rand()*arstd);
    skew=skewstd*randn();
    xguess=xstart+(randn()*xstd);
    yguess=ystart+(randn()*ystd);
    Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];
    
    G=(Kguess')*F*Kguess;
    s=svd(G);
    Ed(i,1)=abs(s(1,1)-s(2,1))/s(1,1);
    
end

d=mean(Ed);
end

function [d]=eigvar(F,w,h)
mscale=30;
[xstart,ystart,fstart,fstd,xstd,ystd,skewstd,arstd]=setupstdSforperturb(mscale,w,h,F);
numTries=2000;


Ed=zeros(numTries,1);


for i=1:numTries
    focal=fstart+(randn()*fstd);
    ar=(1+rand()*arstd);
    skew=skewstd*randn();
    xguess=xstart+(randn()*xstd);
    yguess=ystart+(randn()*ystd);
    Kguess = [focal   skew        xguess;   0      focal*ar   yguess;   0            0             1  ];
    
    G=(Kguess')*F*Kguess;
    [V,D] = eig(G'*G);
    vv=V(:,3)/V(3,3);
    Ed(i,1)=vv(1,1);
    
end

d=std(Ed);
end

function [xstart,ystart,fstart,fstd,xstd,ystd,skewstd,arstd]=setupstdSforperturb(mscale,w,h,F);

xstart=w/2;
ystart=h/2;
fstart=w;
mscale=0.4;

fstd=1* mscale;
xstd=(xstart/200)* mscale;
ystd=(ystart/200)* mscale;
skewstd=0.01*mscale;
arstd=0.01*mscale;

end

function [F,x1,x2]=addspecificnoisetoF(x1,x2,noiselevel)
numCorrs=size(x1,2);


numaffectedpts=numCorrs*noiselevel;
for q=1:numaffectedpts
    
   
    if(q~=numCorrs)
    if(rand()<0.5)
        x2(1,q)=x2(1,q+1); % outlier generation, this is whack and important
        x2(2,q)=x2(2,q+1);
        
    else
        x1(1,q)=x1(1,q+1); % outlier generation, this is whack and important
        x1(2,q)=x1(2,q+1);
    end
    end
end

[F, ~, ~] = fundmatrix(x1, x2);
end
